import {createRouter,createWebHistory} from 'vue-router'
import {useUserStore} from "../stores/user.js";
import {showFailToast, showSuccessToast} from "vant";
import Login from '../pages/login.vue'
import HealthManagement from '../pages/healthManagement.vue'
import Home from '../pages/home.vue'
import ResetPassword from "../pages/resetPassword.vue";
import User from "../pages/user.vue";
import Enroll from "../pages/enroll.vue";
import Service from '../pages/service.vue'
import Reports from '../pages/reports.vue' //报告查询
import Payment from '../pages/payment.vue' //在线缴费页面
import Register from '../pages/register.vue'  //挂号页面
import RegisterPay from '../pages/registerPay.vue'  //挂号支付页面
import Queue from '../pages/queue.vue'
import PatientManagement from "../pages/patientManagement.vue";
import UserInfo from "../pages/userInfo.vue";


const routes=[
	{
		path:'/login',
		name:'Login',
		component:Login,
	},
	{
		path:'/resetPassword',
		name:'ResetPassword',
		component:ResetPassword,
	},
	{
		path:'/patientManagement',
		name:'PatientManagement',
		component:PatientManagement,
	},
	{
		path:'/user',
		name:'User',
		component:User,
	},
	{
		path:'/enroll',
		name:'Enroll',
		component:Enroll,
	},
	{
		path:'/userInfo',
		name:'UserInfo',
		component:UserInfo,
	},
	{
		path:'/healthManagement',
		name:'HealthManagement',
		component:HealthManagement,
	},
	{
		path:'/home',
		name:'Home',
		component:Home,

	},
	{
		path:'/',
		redirect:'/login'
	},
	{	path:"/service",
		component:Service,
	},
	{	path:'/reports',
		component:Reports},
	{	path:'/payment',
		component:Payment,
	},
	{	path: '/register',
		component: Register },
	{	path:'/registerPay',
		component:RegisterPay},	
	{
		path:'/queue',
		component:Queue,},
	]
const router = createRouter({
  history: createWebHistory(),  
  routes  // 应用路由规则
})
const whiteList = ['/login', '/resetPassword', '/enroll']
router.beforeEach((to,from,next)=>{
	const userStore = useUserStore()
	if (whiteList.includes(to.path)) {
		next()
	}else {
		if (userStore.userInfo && userStore.userInfo.user_id) {
			next() // 已登录，允许访问
		} else {
			// 未登录，跳转到登录页
			showFailToast('请先登录')
			next('/login') // 使用 next 而不是 router.replace
		}
	}
})
export default router